package com.jiyun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiyun.entity.Goods;
import com.jiyun.mapper.BrandMapper;
import com.jiyun.mapper.CategoryMapper;
import com.jiyun.mapper.GoodsMapper;
import com.jiyun.service.IGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 聪哥哥
 * @since 2023-06-06
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {

    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    BrandMapper brandMapper;

    @Override
    public Page<Goods> findPage(Page<Goods> goodsPage, Goods g) {
        LambdaQueryWrapper<Goods> qw = new LambdaQueryWrapper<>();
        qw.like(StringUtils.isNotBlank(g.getName()),Goods::getName,g.getName());
        goodsMapper.selectPage(goodsPage,qw);

        for (Goods goods : goodsPage.getRecords()) {
            goods.setCategoryName(categoryMapper.selectById(goods.getCategoryId()).getName());
            goods.setBrandName(brandMapper.selectById(goods.getBrandId()).getName());
        }

        return goodsPage;
    }

    @Override
    public List<Goods> findAllName() {
        List<Goods> goodsList = goodsMapper.selectList(null);
        for (Goods goods : goodsList) {
            goods.setCategoryName(categoryMapper.selectById(goods.getCategoryId()).getName());
            goods.setBrandName(brandMapper.selectById(goods.getBrandId()).getName());
        }
        return goodsList;
    }

    @Override
    public Map zhu() {
        List<Map> list = goodsMapper.selectZhu();
        List<String> x = new ArrayList<>();
        List<Integer> y = new ArrayList<>();
        for (Map map : list) {
            x.add((String)map.get("x"));
            y.add(Integer.parseInt(map.get("y")+""));
        }
        Map map = new HashMap();
        map.put("x",x);
        map.put("y",y);
        return map;
    }

    @Override
    public List<Map> bing() {
        return goodsMapper.selectBing();
    }
}
